package com.wptk.filemanager.ui.file;

import android.os.Bundle;
import android.view.MenuItem;
import android.view.View;
import android.widget.CheckedTextView;
import android.widget.RelativeLayout;

import androidx.annotation.NonNull;
import androidx.appcompat.widget.Toolbar;

import com.google.android.material.tabs.TabLayout;
import com.wptk.filemanager.R;
import com.wptk.filemanager.ad.utils.AnalyticsUtils;
import com.wptk.filemanager.ui.base.BaseActivity;
import com.wptk.filemanager.ui.file.adapter.DocsPageAdapter;
import com.wptk.filemanager.ui.view.ViewPagerNoScroll;

import java.util.ArrayList;
import java.util.List;

public class DocsActivity extends BaseActivity implements OnSelectModelChangeListener{

    private View statusBarDimen;
    private RelativeLayout All;
    private Toolbar toolbar;
    private CheckedTextView cbAll;
    private TabLayout docsTypeTab;
    private ViewPagerNoScroll vp;

    private DocsPageAdapter pageAdapter;

    public static final String[] DOCS_TYPE = new String[]{"All","PDF","Word","Excel","PPT","Other"};
    private List<DocFragment> pages = new ArrayList<>();
    private boolean isSelectedModel = false;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        docsTypeTab.setupWithViewPager(vp);
    }

    @Override
    protected int getLayoutId() {
        return R.layout.activity_docs;
    }

    @Override
    protected void initView() {
        AnalyticsUtils.INSTANCE.logEvent("ckwd");
        statusBarDimen = (View) findViewById(R.id.status_bar_dimen);
        All = (RelativeLayout) findViewById(R.id.All);
        toolbar = (Toolbar) findViewById(R.id.toolbar);
        cbAll = (CheckedTextView) findViewById(R.id.cb_all_toolbar);
        docsTypeTab =  findViewById(R.id.docs_type_tab);
        vp = (ViewPagerNoScroll) findViewById(R.id.vp);

    }

    @Override
    protected void initEvent() {

        toolbar.setTitle(getString(R.string.docs));

        for (String s : DOCS_TYPE) {
            pages.add(DocFragment.newInstance(s,this));
        }
        pageAdapter = new DocsPageAdapter(getSupportFragmentManager(),pages);

        vp.setAdapter(pageAdapter);

        docsTypeTab.addOnTabSelectedListener(new TabLayout.OnTabSelectedListener() {
            @Override
            public void onTabSelected(TabLayout.Tab tab) {

            }

            @Override
            public void onTabUnselected(TabLayout.Tab tab) {
                if (isSelectedModel){
                    pages.get(tab.getPosition()).updateSelectMode(false);
                    onChange(false);
                }
            }

            @Override
            public void onTabReselected(TabLayout.Tab tab) {

            }
        });
        cbAll.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                cbAll.toggle();
                pages.get(vp.getCurrentItem()).notifyCheckAll(cbAll.isChecked());

            }
        });
    }

    @Override
    public boolean onOptionsItemSelected(@NonNull MenuItem item) {
        switch (item.getItemId()){
            case android.R.id.home:
                if (isSelectedModel){
                    onChange(false);
                    cbAll.setChecked(false);
                    return true;
                }
                finish();
        }
        return true;
    }

    @Override
    public void onBackPressed() {
        if(isSelectedModel){
            onChange(false);
        }else{
            super.onBackPressed();
        }
    }

//    public void setSelectModelToolbar(boolean isSelectModel){
//        this.isSelectedModel = isSelectModel;
//        if(isSelectModel){
//            //checkAll.setVisibility(View.VISIBLE);
//            toolbar.setNavigationIcon(R.mipmap.cancle);
//
//        }else{
//            //checkAll.setVisibility(View.GONE);
//            toolbar.setNavigationIcon(R.mipmap.nav_left);
//        }
//    }




    @Override
    public void onChange(boolean isSelect) {
        this.isSelectedModel = isSelect;
        pages.get(vp.getCurrentItem()).updateSelectMode(isSelect);
        if(isSelectedModel){
            toolbar.setNavigationIcon(R.mipmap.cancle);
            cbAll.setVisibility(View.VISIBLE);
            vp.setCanScroll(false);
        }else{
            toolbar.setNavigationIcon(R.mipmap.back);
            cbAll.setVisibility(View.GONE);
            cbAll.setChecked(false);
            vp.setCanScroll(true);
        }
    }
}